home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / perl / perl5a1.lha / perl5alpha1 / do / exec < prev    next >
Encoding:
Text File  |  1992-08-15  |  1.4 KB  |  78 lines

  1. bool
  2. do_exec(cmd)
  3. char *cmd;
  4. {
  5.     register char **a;
  6.     register char *s;
  7.     char flags[10];
  8.  
  9.     /* save an extra exec if possible */
  10.  
  11. #ifdef CSH
  12.     if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
  13.     strcpy(flags,"-c");
  14.     s = cmd+cshlen+3;
  15.     if (*s == 'f') {
  16.         s++;
  17.         strcat(flags,"f");
  18.     }
  19.     if (*s == ' ')
  20.         s++;
  21.     if (*s++ == '\'') {
  22.         char *ncmd = s;
  23.  
  24.         while (*s)
  25.         s++;
  26.         if (s[-1] == '\n')
  27.         *--s = '\0';
  28.         if (s[-1] == '\'') {
  29.         *--s = '\0';
  30.         execl(cshname,"csh", flags,ncmd,(char*)0);
  31.         *s = '\'';
  32.         return FALSE;
  33.         }
  34.     }
  35.     }
  36. #endif /* CSH */
  37.  
  38.     /* see if there are shell metacharacters in it */
  39.  
  40.     /*SUPPRESS 530*/
  41.     for (s = cmd; *s && isALPHA(*s); s++) ;    /* catch VAR=val gizmo */
  42.     if (*s == '=')
  43.     goto doshell;
  44.     for (s = cmd; *s; s++) {
  45.     if (*s != ' ' && !isALPHA(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
  46.         if (*s == '\n' && !s[1]) {
  47.         *s = '\0';
  48.         break;
  49.         }
  50.       doshell:
  51.         execl("/bin/sh","sh","-c",cmd,(char*)0);
  52.         return FALSE;
  53.     }
  54.     }
  55.     New(402,Argv, (s - cmd) / 2 + 2, char*);
  56.     Cmd = nsavestr(cmd, s-cmd);
  57.     a = Argv;
  58.     for (s = Cmd; *s;) {
  59.     while (*s && isSPACE(*s)) s++;
  60.     if (*s)
  61.         *(a++) = s;
  62.     while (*s && !isSPACE(*s)) s++;
  63.     if (*s)
  64.         *s++ = '\0';
  65.     }
  66.     *a = Nullch;
  67.     if (Argv[0]) {
  68.     execvp(Argv[0],Argv);
  69.     if (errno == ENOEXEC) {        /* for system V NIH syndrome */
  70.         do_execfree();
  71.         goto doshell;
  72.     }
  73.     }
  74.     do_execfree();
  75.     return FALSE;
  76. }
  77.  
  78.